/**
 * xdemo.org
 */
package org.xdemo.site.system.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * @作者 Goofy
 * @邮件 252878950@qq.com
 * @日期 2014-1-11
 * @描述  自定义异常解处理器
 */
public class XdemoExceptionHandler implements HandlerExceptionResolver{
	
	private Logger log=Logger.getLogger(XdemoExceptionHandler.class);
	
	private boolean displayException;
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		log.error("访问 ["+request.getRequestURI()+"]发生异常:"+ex.getMessage());
		
		if(displayException)ex.printStackTrace();
		
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("exception", ex.getMessage());
		return new ModelAndView("errorPage/500",model);
	}

	/**
	 * @param displayException the displayException to set
	 */
	public void setDisplayException(boolean displayException) {
		this.displayException = displayException;
	}
	
}
